package com.videoaggre.zcl.utils;

import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhoucl on 2019/5/14 0014.
 */
public class ViewSpanUtil {
    private TextView textView;
    private String showText;
    private String spanText;
    private List<ViewSpanEntity> spanObjects;

    public ViewSpanUtil(TextView textView, String showText, String spanText) {
        this.textView = textView;
        this.showText = showText;
        this.spanText = spanText;
        spanObjects = new ArrayList<>();
    }

    public ViewSpanUtil changeSpanText(String spanText) {
        this.spanText = spanText;
        return this;
    }

    public ViewSpanUtil setBoldSpan() {
        spanObjects.add(new ViewSpanEntity(spanText, new StyleSpan(Typeface.BOLD)));
        return this;
    }

    public ViewSpanUtil setForegroundColorSpan(int color) {
        spanObjects.add(new ViewSpanEntity(spanText, new ForegroundColorSpan(color)));
        return this;
    }

    public ViewSpanUtil setAbsoluteSizeSpan(int textSizePx) {
        spanObjects.add(new ViewSpanEntity(spanText, new AbsoluteSizeSpan(textSizePx, false)));
        return this;
    }

    public ViewSpanUtil setUnderlineSpan() {
        spanObjects.add(new ViewSpanEntity(spanText, new UnderlineSpan()));
        return this;
    }

    public ViewSpanUtil setClickableSpan(ClickableSpan clickableSpan) {
        spanObjects.add(new ViewSpanEntity(spanText, clickableSpan));
        return this;
    }

    public SpannableString get() {
        if (TextUtils.isEmpty(showText) || spanObjects.isEmpty()) return null;
        SpannableString spannableString = new SpannableString(showText);
        for (ViewSpanEntity viewSpanEntity : spanObjects) {
            if (TextUtils.isEmpty(viewSpanEntity.getSpanText())) continue;
            String spanText = viewSpanEntity.getSpanText();
            int start = showText.indexOf(spanText);
            int end = start + spanText.length();
            spannableString.setSpan(viewSpanEntity.getSpan(), start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        return spannableString;
    }

    public void submit() {
        SpannableString spannableString = get();
        if (spannableString != null && textView != null)
            textView.setText(spannableString);
    }

    public static ViewSpanUtil Builder(String showText, String spanText) {
        return new ViewSpanUtil(null, showText, spanText);
    }

    public static ViewSpanUtil Builder(TextView textView, String showText, String spanText) {
        return new ViewSpanUtil(textView, showText, spanText);
    }

    class ViewSpanEntity {
        private String spanText;
        private Object span;

        ViewSpanEntity(String spanText, Object span) {
            this.spanText = spanText;
            this.span = span;
        }

        public String getSpanText() {
            return spanText == null ? "" : spanText;
        }

        public void setSpanText(String spanText) {
            this.spanText = spanText;
        }

        public Object getSpan() {
            return span;
        }

        public void setSpan(Object span) {
            this.span = span;
        }
    }
}
